package com.lanhao.aot.carrierintegration.dpd;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import cn.hutool.http.HttpRequest;

/**
  *  捷克物流商对接
 * @author kunzhou3
 *
 */
public class DPDCZInvokeUtil {
	private static final Logger LOG = LoggerFactory.getLogger(DPDCZInvokeUtil.class);

	public static final String BASE_URL = "https://geoapi.dpd.cz/v1";
	public static final String ME_URL = BASE_URL + "/me";
	public static final String PICKUP_URL = BASE_URL + "/pickup-orders";
	
	public static final String CustomerDSW = "10006803628";
	public static final String API_KEY = "3e012067b1ddc0623b8a5712ebbe4be4";
	
	public static final int ADDRESS_ID = 15769904;
	
	public static void main(String[] args) {
//		me();
		createPickUpOrder();
	}
	
	
	public static void me() {
		String respBody = HttpRequest.get(ME_URL).header("x-api-key", API_KEY).execute().body();
		LOG.info("获取个人信息，返回信息respBody={}", respBody);
	}
	
	public static void createPickUpOrder() {
		JSONArray request = new JSONArray();
		
		JSONObject addressNode = new JSONObject();
//		addressNode.put("date", "2023-11-01");
//		addressNode.put("note", "Call before arrival");
		JSONObject customerAddress = new JSONObject();
		customerAddress.put("it4emId", ADDRESS_ID);
		addressNode.put("customerAddress", customerAddress);
		request.add(addressNode);
		
		JSONObject parcelNode = new JSONObject();
		parcelNode.put("date", "2023-11-01");
		parcelNode.put("note", "Call before arrival");
		JSONObject parcel = new JSONObject();
		parcel.put("parcelNumber", "13815045502677");
		parcelNode.put("parcel", parcel);
		request.add(parcelNode);
		
		JSONObject arbitraryAddressNode = new JSONObject();
		
		JSONObject customer = new JSONObject();
		customer.put("dsw", CustomerDSW);
		
		// 应该是取件地址
		JSONObject arbitraryAddress = new JSONObject();
		JSONObject info = new JSONObject();
		info.put("name1", "KunKun");
//		info.put("name2", "KunKun");
//		JSONObject contact = new JSONObject();
//		contact.put("person", "KunKun");
//		contact.put("phone", "KunKun");
//		contact.put("email", "KunKun");
//		info.put("contact", contact);
		
		JSONObject address = new JSONObject();
		address.put("street", "Example Street 368/23");
		address.put("postalCode", "40001");
		address.put("city", "Praha");
//		address.put("houseNumber", "-");
		
		JSONObject country = new JSONObject();
//		country.put("isoAlpha3", "CZE");
		country.put("isoAlpha2", "CZ");
		address.put("country", country);
		
		arbitraryAddress.put("info", info);
		arbitraryAddress.put("address", address);
		
		arbitraryAddressNode.put("customer", customer);
		arbitraryAddressNode.put("arbitraryAddress", arbitraryAddress);
//		arbitraryAddressNode.put("date", "2023-11-01");
//		arbitraryAddressNode.put("note", "Call before arrival");
		
		request.add(arbitraryAddressNode);
		
		Map<String, String> header = Maps.newHashMap();
		header.put("x-api-key", API_KEY);
		
		String requestBody = request.toString();
		LOG.info("开始请求DPD-CZ标签，请求url={}，参数request={}", PICKUP_URL, requestBody);
		String respBody = HttpRequest.post(PICKUP_URL).addHeaders(header).body(requestBody).execute().body();
		LOG.info("结束调用DPD-CZ标签，返回结果respBody={}", respBody);
		
		// 开始请求DPD-CZ标签，请求url=https://geoapi.dpd.cz/v1/pickup-orders，参数request=[{"customerAddress":{"it4emId":15769904}}]
		// 结束调用DPD-CZ标签，返回结果respBody=[{"id":33447,"statusText":"The pickup order has been created in the DPD systems","statusCode":"Created","specs":{"customerAddress":{"it4emId":15769904},"date":"2023-11-02"}}]

	}
}
